home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / shells / tcshsrc.zoo / tcsh / tw.spell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-17  |  5.9 KB  |  189 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.00/RCS/tw.spell.c,v 3.2 1991/07/15 19:37:24 christos Exp $ */
  2. /*
  3.  * tw.spell.c: Spell check words
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "config.h"
  38. RCSID("$Id: tw.spell.c,v 3.2 1991/07/15 19:37:24 christos Exp $")
  39.  
  40. #include "sh.h"
  41. #include "tw.h"
  42.  
  43. #ifdef __MINT__
  44. extern Char *Lastslash();
  45. #endif
  46.  
  47. extern Char **command_list;
  48. extern int numcommands;
  49.  
  50. /* spell_me : return corrrectly spelled filename.  From K&P spname */
  51. int
  52. spell_me(oldname, oldsize, looking_for_cmd)
  53.     Char   *oldname;
  54.     int     oldsize, looking_for_cmd;
  55. {
  56.     /* The +1 is to fool hp's optimizer */
  57.     Char    guess[FILSIZ + 1], newname[FILSIZ + 1];
  58.     register Char *new = newname, *old = oldname;
  59.     register Char *p, *cp, *ws;
  60.     bool    foundslash = 0;
  61.     int     retval;
  62.  
  63. #ifdef __MINT__
  64.     /* skip drive specification, if any */
  65.     if (*old && old[1] == ':' && is_dirsep(old[2])) {
  66.     *new++ = *old++; *new++ = *old++;
  67.     }
  68. #endif
  69.  
  70.     for (;;) {
  71. #ifdef __MINT__
  72.     while (is_dirsep(*old)) {
  73. #else
  74.     while (*old == '/') {    /* skip '/' */
  75. #endif
  76.         *new++ = *old++;
  77.         foundslash = 1;
  78.     }
  79.     /* do not try to correct spelling of single letter words */
  80.     if (*old != '\0' && old[1] == '\0')
  81.         *new++ = *old++;
  82.     *new = '\0';
  83.     if (*old == '\0') {
  84.         retval = (StrQcmp(oldname, newname) != 0);
  85.         copyn(oldname, newname, oldsize);    /* shove it back. */
  86.         return retval;
  87.     }
  88.     p = guess;        /* start at beginning of buf */
  89.     if (newname[0])        /* add current dir if any */
  90.         for (cp = newname; *cp; cp++)
  91.         if (p < guess + FILSIZ)
  92.             *p++ = *cp;
  93.     ws = p;
  94. #ifdef __MINT__
  95.     for (; !is_dirsep(*old) && *old != '\0'; old++)
  96. #else
  97.     for (; *old != '/' && *old != '\0'; old++)/* add current file name */
  98. #endif
  99.         if (p < guess + FILSIZ)
  100.         *p++ = *old;
  101.     *p = '\0';        /* terminate it */
  102.  
  103.     /*
  104.      * Don't tell t_search we're looking for cmd if no '/' in the name so
  105.      * far but there are later - or it will look for *all* commands
  106.      */
  107.     /* (*should* say "looking for directory" whenever '/' is next...) */
  108. #ifdef __MINT__
  109.     retval = t_search(guess, p, SPELL, FILSIZ,
  110.       looking_for_cmd && (foundslash || !is_dirsep(*old)), 1);
  111. #else
  112.     retval = t_search(guess, p, SPELL, FILSIZ,
  113.               looking_for_cmd && (foundslash || *old != '/'), 1);
  114. #endif
  115.     if (retval >= 4 || retval < 0)
  116.         return -1;        /* hopeless */
  117.     for (p = ws; *new = *p++;)
  118.         new++;
  119.     }
  120. /*NOTREACHED*/
  121. #ifdef notdef
  122.     return (0);            /* lint on the vax under mtXinu complains! */
  123. #endif
  124. }
  125.  
  126. #define EQ(s,t)    (StrQcmp(s,t) == 0)
  127.  
  128. /*
  129.  * spdist() is taken from Kernighan & Pike,
  130.  *  _The_UNIX_Programming_Environment_
  131.  * and adapted somewhat to correspond better to psychological reality.
  132.  * (Note the changes to the return values)
  133.  *
  134.  * According to Pollock and Zamora, CACM April 1984 (V. 27, No. 4),
  135.  * page 363, the correct order for this is:
  136.  * OMISSION = TRANSPOSITION > INSERTION > SUBSTITUTION
  137.  * thus, it was exactly backwards in the old version. -- PWP
  138.  */
  139.  
  140. int
  141. spdist(s, t)
  142.     register Char *s, *t;
  143. {
  144.     for (; (*s & TRIM) == (*t & TRIM); t++, s++)
  145.     if (*t == '\0')
  146.         return 0;        /* exact match */
  147.     if (*s) {
  148.     if (*t) {
  149.         if (s[1] && t[1] && (*s & TRIM) == (t[1] & TRIM) &&
  150.         (*t & TRIM) == (s[1] & TRIM) && EQ(s + 2, t + 2))
  151.         return 1;    /* transposition */
  152.         if (EQ(s + 1, t + 1))
  153.         return 3;    /* 1 char mismatch */
  154.     }
  155.     if (EQ(s + 1, t))
  156.         return 2;        /* extra character */
  157.     }
  158.     if (*t && EQ(s, t + 1))
  159.     return 1;        /* missing character */
  160.     return 4;
  161. }
  162.  
  163. int
  164. spdir(extended_name, tilded_dir, entry, name)
  165.     Char   *extended_name;
  166.     Char   *tilded_dir;
  167.     Char   *entry;
  168.     Char   *name;
  169. {
  170.     Char    path[1024];
  171.     Char   *s;
  172.     Char    oldch;
  173.  
  174.     for (s = name; *s != 0 && (*s & TRIM) == (*entry & TRIM); s++, entry++);
  175.     if (*s == 0 || s[1] == 0 || *entry != 0)
  176.     return 0;
  177.  
  178.     (void) Strcpy(path, tilded_dir);
  179.     oldch = *s;
  180.     *s = '/';
  181.     catn(path, name, sizeof(path) / sizeof(Char));
  182.     if (access(short2str(path), F_OK) == 0) {
  183.     (void) Strcpy(extended_name, name);
  184.     return 1;
  185.     }
  186.     *s = oldch;
  187.     return 0;
  188. }
  189.